var user=getUserEntityFromCache();

if(user.mobile==null||user.mobile==undefined||user.mobile==""){
    window.location.href="/user/mobilebind";
}

$(function () {
    init();
    addEvent();
    if (!user.isLogin()) {//没登录
        //微信授权回调回来
        if(code!=undefined&&code!=null&&code.length>0&&state=="pocket"){
            getWxUserInfo();
        }
        else {
            user.clearUserInfoCache();
            getUserAuthorize();
        }
    }
    else {
        getHouseData();
    }
})

function getHouseData() {
    getAttentionList();
}

function init() {

}

function addEvent() {
    $("#house_list").on("click",".coupon_item",function () {
        window.location.href="/house/detail?hid="+$(this).attr("data-id");
    });

    $("#house_list").on("click",".house_return",function () {
        window.location.href="https://mp.weixin.qq.com/s?__biz=MzUxMDUwNDk5OQ==&mid=2247483738&idx=1&sn=8d42401efe80d71f1fa4ad928bfcd9c0&chksm=f900b812ce773104296101798aded368bb54da81d3761b36bd79355a24b13aae90cce77a9f86#rd";
        e.preventDefault();
    });
}

function getAttentionList(){
    $.ajax({
        type: "get",
        async: true,
        url: "/v1/coupon/my-list",
        data: {uid:user.uid},
        dataType: "json",
        beforeSend:function(){
            showLoading();
        },
        success: function(data){
            if(data.code=="success"){
                var data_list=data.data.list;
                appendHouseData(data_list,data.data.nextPage);
            }
            else {
                showAlert(data.message);
            }
            hideLoading();
        },
        error: function(){
            hideLoading();
        }
    });
}

function appendHouseData(list,next_page) {
    if(list!=null&&list.length>0){
        var house_html="";
        var item;
        for(var i=0;i<list.length;i++){
            item=list[i];

            house_html+='<li class="coupon_item list-unstyled">\n' +
                '            <div class="coupon_info">\n' ;
                if(item.used=="0"){
                    house_html+='                <img class="coupon_bg" src="../images/coupon.png">\n';
                }
                else {
                    house_html+='                <img class="coupon_bg" src="../images/coupon_gray.png">\n';
                }

            house_html+='                <p class="coupon_price">¥'+item.denomination+'</p>\n' +
                '                <p class="coupon_price_tip">抵用金额</p>\n' +
                '                <p class="coupon_code">编号：'+item.code+'</p>\n' +
                '                <img class="coupon_logo" src="../images/logo_small.png">\n' +
                '            </div>\n' +
                '        </li>';
        }

        if(house_html!=null&&house_html.length>0){
            $("#coupon_list").append(house_html);
        }
    }
}

function getWxUserInfo() {
    getUserInfo(function (data) {
        if(data&&data.openid){
            user=new User(data.openid,data.mobile,data.nickname,data.headimgurl,data.uid,data.role,data.referee,data.parent_code,data.redpacket_money,data.redpacket_num,data.isgetredpacket);
            user.cacheUserInfo(user.openid,user.nickname,user.mobile,user.avatar,user.uid,data.role,data.referee,data.parent_code,data.redpacket_money,data.redpacket_num,data.isgetredpacket);
            if(user.mobile==null||user.mobile==undefined||user.mobile==""){
                window.location.href="/user/mobilebind";
            }
            else {
                getHouseData();
            }
        }
        else {
            showAlert(data);
        }
    });
}

